<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/11
 * Time: 20:22
 */
namespace App\Http\Controllers\Api\V1_9_1;

use App\Http\Controllers\Controller;
use App\Services\AlipayValidateCardInfo\BankCardInfoService;
use App\Services\UploadOSSorStorage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CardLogoController extends Controller
{
    public function uploadPic(Request $request)
    {
        //上传新图片
        if ($request->image_type == 1) {
            $uploadFilePath = $this->upload('alioss', 'gallery/shuffling', $request->pic);
        }
        if ($request->image_type == 2) {
            $uploadFilePath = $this->upload('alioss', 'gallery/share', $request->pic);
        }
        if (is_object($uploadFilePath)) return $uploadFilePath;
        //删除旧图片
        if (!empty($request->id)) {
            $id = $request->id;
            $gallery = DB::table('gallery')->find($id);
            if (!empty($gallery)) {
                $uploadOSSorStorage = new UploadOSSorStorage();
                $uploadOSSorStorage->deleteOSSorStorageLocal($gallery->shuffling_images, "alioss");
                //更新图片
                DB::table('gallery')->where('id', $id)->update(['shuffling_images' => $uploadFilePath]);
            }
            if ($id > 0) {
                echo 'id:' . $id;
                echo '<br>';
                echo 'fuck success!!';
            } else {
                echo 'fuck fail!!';
            }
        } else {
            $id = DB::table('gallery')->insertGetId([
                'title' => $request->title,
                'image_type' => $request->image_type,
                'shuffling_images' => $uploadFilePath,
                'jump_url' => $request->jump_url,
            ]);
            if ($id > 0) {
                echo 'id:' . $id;
                echo '<br>';
                echo 'fuck success!!';
            } else {
                echo 'fuck fail!!';
            }
        }
    }



    public function uploadCardLogo(Request $request)
    {

//        $a = new BankCardInfoService();
//        //6222520717848556
//        //6222621312044443333
//        //6221550810564603
//        $b = $a->getBankInfo('6221550810564603');
//        dd($b);
//        $bankList = BankCardInfoService::getBankList();
//        foreach ($bankList as $key=>$value) {
//            $isExist = DB::table('card_logo')->where('bank_code', $key)->first();
//            if (!empty($isExist)) {
//                DB::table('card_logo')->where('bank_code', $key)->update(['bank_description' => $value, 'update_time' => time()]);
////                DB::table('card_logo')->insertGetId([
////                    'card_logo_url' => '',
////                    'bank_code' => $key,
////                    'bank_description' => $value,
////                    'create_time' => time(),
////                    'update_time' => time(),
////                ]);
//            }
//        }
//        dd($bankList);
        //上传新图片
        $uploadFilePath = $this->upload('alioss', 'card_logo', $request->card_logo_url);
        if (is_object($uploadFilePath)) return $uploadFilePath;
//        $id = DB::table('card_logo')->insertGetId([
//            'card_logo_url' => $uploadFilePath,
//            'bank_code' => $request->code,
//            'bank_description' => $request->name,
//            'create_time' => time(),
//            'update_time' => time(),
//        ]);
        $id = DB::table('card_logo')->where('id', $request->id)->update(['card_logo_url' => $uploadFilePath]);
        echo getOssImageUrl($uploadFilePath);
        echo '<br>';
        echo 'fuck success!!';
//        if ($id > 0) {
//
//        } else {
//            echo 'fuck fail!!';
//        }
    }

    /**
     * 上传图片
     * @param $storageType
     * @param $folder
     * @param $file
     * @return bool|\Illuminate\Http\JsonResponse|string
     */
    public function upload($storageType, $folder, $file)
    {
        $uploadOSSorStorage = new UploadOSSorStorage();
        // 本地文件是否上传成功
        if (!$uploadOSSorStorage->isValidFile($file)) die('无效文件');
        //上传文件类型
        if (!$uploadOSSorStorage->isValidFileType($file)) die('图片格式不支持');
        //验证文件大小是否超出限制
        $fileLimitSize = config('filesystems.disks.image_size');
        if (!$uploadOSSorStorage->isValidFileSize($file, $fileLimitSize)) die('图片大小超出限制');
        $isUpload = $uploadOSSorStorage->uploadOSSorStorageLocal($file, $fileLimitSize, $storageType, $folder);
        if (!$isUpload) {
            die('图片上传失败');
        }
        return $isUpload;
    }
}